package com.atguigu.day07;

import org.apache.flink.contrib.streaming.state.EmbeddedRocksDBStateBackend;
import org.apache.flink.contrib.streaming.state.RocksDBStateBackend;
import org.apache.flink.runtime.state.filesystem.FsStateBackend;
import org.apache.flink.runtime.state.hashmap.HashMapStateBackend;
import org.apache.flink.runtime.state.memory.MemoryStateBackend;
import org.apache.flink.runtime.state.storage.FileSystemCheckpointStorage;
import org.apache.flink.runtime.state.storage.JobManagerCheckpointStorage;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;

import java.io.IOException;

public class Flink09_StateBackend {
    public static void main(String[] args) throws IOException {
        //1.获取流的执行环境
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();

        //1.13之前版本写法
        //内存级别
        //如果这里面指定了CheckPoint路径，则做CheckPoint的时候状态会存在指定的路径中，可以是hdfs，所以就不一定是在JobManager内存中了
        env.setStateBackend(new MemoryStateBackend());

        //文件系统
        env.setStateBackend(new FsStateBackend("hdfs://hadoop102:8020/flink/checkpoints/fs"));

        //rocksDB
        env.setStateBackend(new RocksDBStateBackend("hdfs://hadoop102:8020/flink/checkpoints/rocksdb"));


        //1.13之后写法
        //内存级别
        env.setStateBackend(new HashMapStateBackend());
        //CheckPoint时状态存在哪
        env.getCheckpointConfig().setCheckpointStorage(new JobManagerCheckpointStorage());

        //文件级别
        env.setStateBackend(new HashMapStateBackend());
        //以下两种写法都可以，上面一种写法可以设置更多的参数，比如写缓存大小，下面这种写法没办法设置其他参数
        env.getCheckpointConfig().setCheckpointStorage(new FileSystemCheckpointStorage("hdfs://hadoop102:8020/flink/checkpoints/fs"));
        env.getCheckpointConfig().setCheckpointStorage("hdfs://hadoop102:8020/flink/checkpoints/fs");

        //RocksDb
        env.setStateBackend(new EmbeddedRocksDBStateBackend());
        env.getCheckpointConfig().setCheckpointStorage(new FileSystemCheckpointStorage("hdfs://hadoop102:8020/flink/checkpoints/rocksdb"));
        //或者
        env.getCheckpointConfig().setCheckpointStorage("hdfs://hadoop102:8020/flink/checkpoints/rocksdb");
    }
}
